<template>
	<view class="content">
		<!-- 导航栏 -->
		<u-navbar :is-back="true" :title="title" :background="background" :title-color="titleColor"></u-navbar>
		<view class="wrap">
			<view class="u-tabs-box">
				<u-tabs-swiper activeColor="#f29100" ref="tabs" :list="list" :current="current" @change="change"
					:is-scroll="false" swiperWidth="750"></u-tabs-swiper>
			</view>
			<swiper class="swiper-box" :current="swiperCurrent" @transition="transition"
				@animationfinish="animationfinish">
				<swiper-item class="swiper-item">
					<scroll-view scroll-y style="height: 100%;width: 100%;" @scrolltolower="reachBottom">
						<view class="page-box">
							<view class="shop" v-if="dataList1.length > 0">
								<view class="item" @click="onClick($event,res.comId)" v-for="(res, index) in  dataList1" :key="res.comId">
									<view class="left">
										<image :src="'http://localhost:8088'+res.imgUrl.split(',')[0]" mode="aspectFill"></image>
									</view>
									<view class="content">
										<view class="title u-line-2">{{res.comName}}</view>
										<view class="type">{{res.typeName}}</view>
										<view class="delivery-time">发布时间 {{$u.timeFormat(res.createTime, 'yyyy-mm-dd')}}</view>
									</view>
									<view class="right">
										<view class="price">
											￥{{ priceInt(res.soldMoney) }}
											<text class="decimal">.{{ priceDecimal(res.soldMoney) }}</text>
										</view>
										<view class="number">{{ res.oriMoney }}</view>
									</view>
								</view>
								<u-loadmore :status="loadStatus[0]" bgColor="#f2f2f2"></u-loadmore> 
							</view>
							<view v-if="dataList1.length <= 0">
								<view class="centre">
									<image src="../../static/noData.png" mode="aspectFit"></image>
									<view class="explain">
										您还没有相关的闲置
										<view class="tips">可以去发布您的闲置</view>
									</view>
									<view class="btn" @click="toIssue()">发布闲置</view>
								</view>
							</view>
						</view>
					</scroll-view>
				</swiper-item>
				<swiper-item class="swiper-item">
					<scroll-view scroll-y style="height: 100%;width: 100%;" @scrolltolower="reachBottom">
						<view class="page-box">
							<view class="shop" v-if="dataList2.length > 0">
								<view class="item" @click="onClick($event,res.comId)" v-for="(res, index) in  dataList2" :key="res.comId">
									<view class="left">
										<image :src="'http://localhost:8088'+res.imgUrl.split(',')[0]" mode="aspectFill"></image>
									</view>
									<view class="content">
										<view class="title u-line-2">{{res.comName}}</view>
										<view class="type">{{res.typeName}}</view>
										<view class="delivery-time">出售时间 {{$u.timeFormat(res.endTime, 'yyyy-mm-dd')}}</view>
									</view>
									<view class="right">
										<view class="price">
											￥{{ priceInt(res.soldMoney) }}
											<text class="decimal">.{{ priceDecimal(res.soldMoney) }}</text>
										</view>
										<view class="number">{{ res.oriMoney }}</view>
									</view>
								</view>
								<u-loadmore :status="loadStatus[1]" bgColor="#f2f2f2"></u-loadmore>
							</view>
							<view v-if="dataList2.length <= 0">
								<view class="centre">
									<image src="../../static/noData.png" mode="aspectFit"></image>
									<view class="explain">
										您还没有出售的物品
										<view class="tips">抓紧沟通吧(*^_^*)</view>
									</view>
								</view>
							</view>
						</view>
					</scroll-view>
				</swiper-item>
			</swiper>
		</view>
	</view>
</template>

<script>
	import Vue from 'vue'
	import {
		mapGetters
	} from 'vuex'
	export default {
		data() {
			return {
				title: "我的闲置",
				titleColor: "#330867",
				background: {
					// 渐变色
					backgroundImage: 'linear-gradient(to right, #f78ca0 0%, #f9748f 19%, #fd868c 60%, #fe9a8b 100%)'
				},
				list: [{
						name: '待出售'
					},
					{
						name: '已出售'
					}
				],
				dataList1:[],
				dataList2:[],
				// 查询参数
				queryParams1: {
					pageNum: 1,
					pageSize: 10,
					comStatus: '1'
				},
				// 查询参数
				queryParams2: {
					pageNum: 1,
					pageSize: 10,
					comStatus: '4'
				},
				pageTotal1:0,
				pageTotal2:0,
				current: 0,
				swiperCurrent: 0,
				tabsHeight: 0,
				dx: 0,
				loadStatus: ['loadmore', 'loadmore']
			}
		},
		computed: {
			...mapGetters(['getUserInfo']),
			// 价格小数
			priceDecimal() {
				return val => {
					if (val !== parseInt(val)) return val.slice(-2);
					else return '00';
				};
			},
			// 价格整数
			priceInt() {
				return val => {
					if (val !== parseInt(val)) return val.split('.')[0];
					else return val;
				};
			}
		},
		onLoad() {
			this.getDataList1();
			this.getDataList2();
		},
		methods: {
			getDataList1(){
				this.queryParams1.pageNum = 1;
				Vue.prototype.$u.api.selectMyCommodity({
					pageNum: this.queryParams1.pageNum,
					pageSize: this.queryParams1.pageSize,
					userId: this.getUserInfo.userId,
					comStatus: this.queryParams1.comStatus
				}).then(res => {
					if(res.total < this.queryParams1.pageSize){
						this.loadStatus[0] = 'nomore'
					}
					this.pageTotal1 = Math.ceil(res.total/this.queryParams1.pageSize);
					if(res.code == 200 && res.rows.length > 0){
						this.dataList1 = res.rows
					}else{
						console.log(res)
					}
				})
			},
			getDataList2(){
				this.queryParams2.pageNum = 1;
				Vue.prototype.$u.api.selectMyCommodity({
					pageNum: this.queryParams2.pageNum,
					pageSize: this.queryParams2.pageSize,
					userId: this.getUserInfo.userId,
					comStatus: this.queryParams2.comStatus
				}).then(res => {
					if(res.total < this.queryParams2.pageSize){
						this.loadStatus[1] = 'nomore'
					}
					this.pageTotal2 = Math.ceil(res.total/this.queryParams2.pageSize);
					if(res.code == 200 && res.rows.length > 0){
						this.dataList2 = res.rows
					}else{
						console.log(res)
					}
				})
			},
			reachBottom() {
				if(this.current == 0){
					this.loadStatus[0] = 'loading';
					this.queryParams1.pageNum = ++this.queryParams1.pageNum;
					if(this.queryParams1.pageNum <= this.pageTotal1){
						setTimeout(()=>{
							Vue.prototype.$u.api.selectMyCommodity({
								pageNum: this.queryParams1.pageNum,
								pageSize: this.queryParams1.pageSize,
								userId: this.getUserInfo.userId,
								comStatus: this.queryParams1.comStatus
							}).then(res => {
								if(res.code == 200 && res.rows.length > 0){
									this.dataList1 = this.dataList1.concat(res.rows)
									this.loadStatus[0] = 'loadmore';
								}else{
									this.loadStatus[0] = 'nomore'
								}
							})
						},2000)
					}else {
						this.loadStatus[0] = 'nomore'
					}
				}else{
					this.loadStatus[1] = 'loading';
					this.queryParams2.pageNum = ++this.queryParams2.pageNum;
					if(this.queryParams2.pageNum <= this.pageTotal2){
						setTimeout(()=>{
							Vue.prototype.$u.api.selectMyCommodity({
								pageNum: this.queryParams2.pageNum,
								pageSize: this.queryParams2.pageSize,
								userId: this.getUserInfo.userId,
								comStatus: this.queryParams2.comStatus
							}).then(res => {
								if(res.code == 200 && res.rows.length > 0){
									this.dataList2 = this.dataList2.concat(res.rows)
									this.loadStatus[1] = 'loadmore';
								}else{
									this.loadStatus[1] = 'nomore'
								}
							})
						},2000)
					}else {
						this.loadStatus[1] = 'nomore'
					}
				}
			},
			onClick(event,comId){
				uni.navigateTo({
				    url: '/pages/detail/detail?comId='+comId
				});
			},
			toIssue() {
				uni.navigateTo({
					url: '/pages/issue/issue'
				});
			},
			// tab栏切换
			change(index) {
				this.swiperCurrent = index;
			},
			transition({
				detail: {
					dx
				}
			}) {
				this.$refs.tabs.setDx(dx);
			},
			animationfinish({
				detail: {
					current
				}
			}) {
				this.$refs.tabs.setFinishCurrent(current);
				this.swiperCurrent = current;
				this.current = current;
			}
		}
	}
</script>

<style scoped lang="scss">
	.wrap {
		display: flex;
		flex-direction: column;
		height: calc(100vh - var(--window-top));
		width: 100%;
	}

	.swiper-box {
		flex: 1;
	}

	.swiper-item {
		height: 100%;
	}

	.shop {
		width: 710rpx;
		background-color: #ffffff;
		margin: 20rpx auto;
		border-radius: 20rpx;
		box-sizing: border-box;
		padding: 20rpx;
		font-size: 28rpx;

		.item {
			display: flex;
			margin: 20rpx 0 0;

			.left {
				margin-right: 20rpx;

				image {
					width: 200rpx;
					height: 200rpx;
					border-radius: 10rpx;
				}
			}

			.content {
				.title {
					font-size: 28rpx;
					line-height: 50rpx;
				}

				.type {
					margin: 10rpx 0;
					font-size: 24rpx;
					color: $u-tips-color;
				}

				.delivery-time {
					color: #e5d001;
					font-size: 24rpx;
				}
			}

			.right {
				margin-left: 10rpx;
				padding-top: 20rpx;
				text-align: right;

				.decimal {
					font-size: 24rpx;
					margin-top: 4rpx;
				}

				.number {
					color: $u-tips-color;
					font-size: 24rpx;
				}
			}
		}
	}
	.centre {
		text-align: center;
		margin: 200rpx auto;
		font-size: 32rpx;
		image {
			width: 164rpx;
			height: 164rpx;
			border-radius: 50%;
			margin-bottom: 20rpx;
			padding: 5rpx;
		}
		.tips {
			font-size: 24rpx;
			color: #999999;
			margin-top: 20rpx;
		}
		.btn {
			margin: 80rpx auto;
			width: 200rpx;
			border-radius: 32rpx;
			line-height: 64rpx;
			color: #ffffff;
			font-size: 26rpx;
			background: linear-gradient(270deg, rgba(249, 116, 90, 1) 0%, rgba(255, 158, 1, 1) 100%);
		}
	}
</style>
